package com.qm.console.component.util;

import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Component
public class DateUtil {

    public String GetFormatDate(Date date, String formatString) {
        String temp_str = "";
        SimpleDateFormat sdf = new SimpleDateFormat(formatString);
        temp_str = sdf.format(date);
        return temp_str;
    }

    /**
     * 判断传入日期是否晚于 （当前日期 - n天）
     * 例如: isAfterNDaysBefore(new Date(), 0)
     * @return
     */
    public boolean isAfterNDaysBefore(Date date, long n) {
        try{
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            //获取
            Date today = simpleDateFormat.parse(simpleDateFormat.format(new Date()));
            today = Date.from(today.toInstant().minusSeconds(24 * 60 * 60 * n));
            return !date.before(today);
        } catch (Exception e) {
            return false;
        }
    }

    public Long DateToTimestamp(String s){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try{
            Date date = simpleDateFormat.parse(s);
            long ts = date.getTime();
            return ts/1000;
        }catch (Exception e){
            e.printStackTrace();
            return -1l;
        }
    }

    public Long dateToStamp(String datetime,String dateformat){
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dateformat);
        try {
            Date date = simpleDateFormat.parse(datetime);
            long ts = date.getTime();
            return ts;
        }
        catch (Exception e){
            return 0l;
        }
    }

    /**
     * <li>功能描述：时间相减得到天数
     * @param beginDateStr
     * @param endDateStr
     * @return
     * long
     * @author Administrator
     */
    public long getDaySub(String beginDateStr,String endDateStr)
    {
        long day=0;
        java.text.SimpleDateFormat format = new java.text.SimpleDateFormat("yyyy-MM-dd");
        java.util.Date beginDate;
        java.util.Date endDate;
        try
        {
            beginDate = format.parse(beginDateStr);
            endDate= format.parse(endDateStr);
            day=(endDate.getTime()-beginDate.getTime())/(24*60*60*1000);
            //System.out.println("相隔的天数="+day);
        } catch (ParseException e)
        {
            // TODO 自动生成 catch 块
            e.printStackTrace();
        }
        return day;
    }

    /**
     * 获取指定时间到现在的所有月份
     */
    public List<String> listMonths(Calendar from) {
        return listMonths(from, Calendar.getInstance());
    }

    /**
     * 获取指定时间范围内的所有月份
     */
    public List<String> listMonths(Calendar from, Calendar to) {
        List<String> allMonths = new ArrayList<>();
        int delta = (to.get(Calendar.YEAR) - from.get(Calendar.YEAR)) * 12;
        delta += to.get(Calendar.MONTH) - from.get(Calendar.MONTH);

        for (int i = 0; i <= delta; i++) {
            Calendar temp = Calendar.getInstance();
            temp.set(Calendar.DAY_OF_MONTH, 1);
            temp.set(Calendar.YEAR, from.get(Calendar.YEAR));
            temp.set(Calendar.MONTH, from.get(Calendar.MONTH) + i);
            String format = temp.get(Calendar.MONTH) >= 9 ? "%s_%d" : "%s_0%d";
            String result = String.format(format, temp.get(Calendar.YEAR), temp.get(Calendar.MONTH) + 1);
            allMonths.add(result);
        }
        return allMonths;
    }

    /**
     * <li>功能描述：获取两个日期时间断的所有日期列表
     * @param beginDateStr
     * @param endDateStr
     * @return
     * long
     * @author Administrator
     */
    public List<String> getnDayList(String beginDateStr,String endDateStr)
    {
        List<String> dateList = new ArrayList<>();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        int nDays = (int)(this.getDaySub(beginDateStr,endDateStr));
        for(int i=0;i<=nDays;i++)
        {
            Date date = this.getDeteBydelta(i, beginDateStr);
            String strDate = sdf.format(date);
            dateList.add(strDate);
        }
        return dateList;
    }

    /**
     * 获取前n天的map
     * @param
     * @return
     */
    public Map<String,Long> GetNDaysMap(String beginDateStr,String endDateStr)
    {
        int nDays = (int)(this.getDaySub(beginDateStr,endDateStr));
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Map<String,Long> dateValueMap = new LinkedHashMap<String, Long>();
        for (int i = 0; i <= nDays; i++)
        {
            Date date = this.getDeteBydelta(i, beginDateStr);
            String result = format.format(date);
            dateValueMap.put(result,(long)0);
        }
        return dateValueMap;
    }

    public Map<String,Double> GetNDaysDoubleMap(String beginDateStr,String endDateStr)
    {
        int nDays = (int)(this.getDaySub(beginDateStr,endDateStr));
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Map<String,Double> dateValueMap = new LinkedHashMap<String, Double>();
        for (int i = 0; i <= nDays; i++)
        {
            Date date = this.getDeteBydelta(i, beginDateStr);
            String result = format.format(date);
            dateValueMap.put(result, Double.valueOf(0));
        }
        return dateValueMap;
    }

    /**
     * 获取当天的第前n天
     * @param n
     */
    public Date getDeteBydelta(int n)
    {
        Date date=new Date();//取时间
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(calendar.DATE,n*-1);//把日期往后增加一天.整数往后推,负数往前移动
        date = calendar.getTime(); //这个时间就是日期往后推一天的结果
        return date;
    }


    //获取指定日期的前n天
    public String getDeteBydelta(String daystr,int n)
    {
        SimpleDateFormat simpleDateFormat =new SimpleDateFormat("yyyy-MM-dd");
        String result="";
        try
        {
            Date date=simpleDateFormat.parse(daystr);
            Calendar calendar = new GregorianCalendar();
            calendar.setTime(date);
            calendar.add(calendar.DATE,n*-1);
            date = calendar.getTime(); //这个时间就是日期往后推一天的结果
            result = simpleDateFormat.format(date);
        }catch (Exception e){

        }
        return result;
    }


    /**
     * 获取指定天的第后n天
     * @param n
     */
    public Date getDeteBydelta(int n,String curdatestr)
    {
        java.text.SimpleDateFormat format = new java.text.SimpleDateFormat("yyyy-MM-dd");

        Calendar calendar = new GregorianCalendar();
        try {
            calendar.setTime(format.parse(curdatestr));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        calendar.add(calendar.DATE,n);//把日期往后增加一天.整数往后推,负数往前移动
        Date date = calendar.getTime(); //这个时间就是日期往后推一天的结果
        return date;
    }



    public String getDeteBydeltaString(int n)
    {
        Date date=this.getDeteBydelta(n);
        return this.GetFormatDate(date, "yyyy-MM-dd");
    }


    public String getCurrentTime(){
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(date);
    }

    public String getCurrentTime(String strFormat){
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat(strFormat);
        return sdf.format(date);
    }

    public String getDeltaDate(int i,String strFormat){
        Date date = getDeteBydelta(i);
        SimpleDateFormat sdf = new SimpleDateFormat(strFormat);
        return sdf.format(date);
    }


    public int calculateTimeDiff(String startTime,String endTime,String format) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        int result = 0;
        try {
            Date startDate = simpleDateFormat.parse(startTime);
            Date endDate = simpleDateFormat.parse(endTime);
            long startday = startDate.getTime();
            long endday = endDate.getTime();
            result = (int) ((endday - startday) / (1000 * 60 * 60 * 24));

        } catch (Exception e) {

        }
        return result;

    }
    public String dateToStr(Date date)
    {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(date);

    }

    public String dateToTimestampStr(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(date);
    }

    public Date strToDate(String dateString) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            return sdf.parse(dateString);
        } catch (ParseException e) {
            return null;
        }
    }
}
